package com.lysh.http.api;

import com.lysh.http.enums.ErrorMessage;
import com.lysh.http.exception.SDKException;
import com.lysh.http.utils.SignUtils;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liyishan
 * @date 2023/4/19 11:54
 */

public abstract class AbstractApiParams implements ApiParams {
    private String appCode;
    private String appSecret;
    private String version;
    //private String token;
    //private String timestamp;
    private String path;

    public AbstractApiParams() {
    }

    public Map<String, Object> toMapParams() throws SDKException {
        Map<String, Object> params = new HashMap();

        try {
            Class<? extends AbstractApiParams> clazz = this.getClass();
            Field[] fields = clazz.getDeclaredFields();
            int length = fields.length;

            for (int i = 0; i < length; ++i) {
                Field field = fields[i];
                String fieldName = field.getName();
                if (!field.isSynthetic()) {
                    Method method;
                    if (fieldName.equals("fileList")) {
                        method = clazz.getMethod("get".concat(this.capitalizeName(fieldName)));
                        List<File> list = (List) method.invoke(this);

                        for (int j = 0; j < list.size(); ++j) {
                            params.put(j + 1 + "", list.get(j));
                        }
                    } else if (!fieldName.equals("serialVersionUID")) {
                        method = clazz.getMethod("get".concat(this.capitalizeName(fieldName)));
                        params.put(fieldName, method.invoke(this));
                    }
                }
            }

            params.entrySet().removeIf((entry) -> {
                return entry.getValue() == null || entry.getValue() instanceof String && "".equals(entry.getValue().toString());
            });
            this.addCommonMapParams(params);
            return params;
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException var11) {
            throw new SDKException(var11, ErrorMessage.TOPARAM_ERROR);
        }
    }

    protected void addCommonMapParams(Map<String, Object> params) {
        params.put("appCode", this.appCode);
        params.put("version", this.version);
        //params.put("timestamp", this.timestamp);
        //params.put("token", this.token);
    }

    private String capitalizeName(String name) {
        char[] ch = name.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }

        return new String(ch);
    }

    public void setCommonParams(String appCode, String path, String version, String appSecret) {
        this.appCode = appCode;
        this.version = version;
        this.path = path;
        this.appSecret = appSecret;
        //this.timestamp = this.getTimeStamp();
        //this.token = this.createToken();
    }

    protected String createToken() {
        try {
            Map<String, Object> map = this.toMapParams();
            map.entrySet().removeIf((entry) -> {
                return entry.getValue() != null && (entry.getValue() instanceof byte[] || entry.getValue() instanceof File);
            });
            return SignUtils.signature(map, this.path, this.appSecret);
        } catch (Exception var2) {
            throw new RuntimeException(var2);
        }
    }

    public String needMD5Str() {
        return "";
    }

    public String joinTimeAfter() {
        return "";
    }

    public String specialStr() {
        return "";
    }

}
